//
//  FKRefundInfoCell.m
//  WanTao
//
//  Created by Macbook Pro on 2019/4/3.
//  Copyright © 2019 Macbook Pro. All rights reserved.
//

#import "FKRefundInfoCell.h"

@interface FKRefundInfoCell ()

@property (nonatomic, weak) UIImageView *picView;
@property (nonatomic, weak) UILabel *goodsNameLabel;
@property (nonatomic, weak) UILabel *goodsDescLabel;
@property (nonatomic, strong) NSArray *valueLabels;

@end

@implementation FKRefundInfoCell

- (void)setupSubviews
{
    [super setupSubviews];
    self.selectionStyle = UITableViewCellSelectionStyleNone;
    
    UILabel *titleLabel = [UILabel fk_labelWithFont:fk_adjustFont(14) textColor:fkColor333333 text:@"退款信息"];
    [self.contentView addSubview:titleLabel];
    
    UIView *goodsView = [[UIView alloc] init];
    [self.contentView addSubview:goodsView];
    goodsView.backgroundColor = [UIColor groupTableViewBackgroundColor];
    
    UIImageView *picView = [[UIImageView alloc] init];
    [goodsView addSubview:picView];
    self.picView = picView;
    
    UILabel *goodsNameLabel = [UILabel fk_labelWithFont:fk_adjustFont(14) textColor:fkColor333333];
    [goodsView addSubview:goodsNameLabel];
    self.goodsNameLabel = goodsNameLabel;
    goodsNameLabel.numberOfLines = 2;
    
    UILabel *goodsDescLabel = [UILabel fk_labelWithFont:fk_adjustFont(12) textColor:fkColor666666];
    [goodsView addSubview:goodsDescLabel];
    self.goodsDescLabel = goodsDescLabel;
    
    NSArray *titles = @[@"退款原因:", @"退款金额:", @"申请件数:", @"申请时间:", @"退款编号:"];
    CGFloat beginY = fk_adjustW(136) + fk_adjustW(4.5);
    CGFloat leftX = 12;
    CGFloat labelH = fk_adjustW(24);
    CGFloat leftW = [titles.firstObject sizeWithAttributes:@{NSFontAttributeName : fk_adjustFont(12)}].width;
    CGFloat rightX = leftX + leftW + 4;
    CGFloat rightW = fkScreenW - rightX - 12;
    NSMutableArray *valueLabels = [NSMutableArray array];
    for (NSInteger index = 0; index < titles.count; index++) {
        UILabel *leftLabel = [UILabel fk_labelWithFont:fk_adjustFont(12) textColor:fkColor999999 text:titles[index]];
        [self.contentView addSubview:leftLabel];
        
        UILabel *rightLabel = [UILabel fk_labelWithFont:fk_adjustFont(12) textColor:fkColor999999];
        [self.contentView addSubview:rightLabel];
        [valueLabels addObject:rightLabel];
        
        CGFloat labelY = beginY + index * labelH;
        leftLabel.frame = CGRectMake(leftX, labelY, leftW, labelH);
        rightLabel.frame = CGRectMake(rightX, labelY, rightW, labelH);
    }
    self.valueLabels = valueLabels;
    
    UIView *separator = [[UIView alloc] init];
    [self.contentView addSubview:separator];
    separator.backgroundColor = [UIColor groupTableViewBackgroundColor];
    
    UIButton *callBtn = [UIButton fk_btnWithTarget:self action:@selector(callBtnClicked) font:fk_adjustFont(14) normalTitle:@"拨打电话" normalTitleColor:fkColor666666 selectedTitle:nil selectedTitleColor:nil normalImg:[UIImage imageNamed:@"dial"] selectedImg:nil bgImg:nil selectedBgImg:nil];
    [self.contentView addSubview:callBtn];
    callBtn.titleEdgeInsets = UIEdgeInsetsMake(0, 6, 0, 0);

    [titleLabel mas_makeConstraints:^(MASConstraintMaker *make) {
        make.left.offset(12);
        make.top.offset(0);
        make.height.offset(fk_adjustW(46));
    }];
    [goodsView mas_makeConstraints:^(MASConstraintMaker *make) {
        make.top.equalTo(titleLabel.mas_bottom);
        make.left.right.offset(0);
        make.height.offset(fk_adjustW(90));
    }];
    [picView mas_makeConstraints:^(MASConstraintMaker *make) {
        make.width.height.offset(fk_adjustW(70));
        make.left.offset(12);
        make.centerY.offset(0);
    }];
    [goodsNameLabel mas_makeConstraints:^(MASConstraintMaker *make) {
        make.left.equalTo(picView.mas_right).offset(8);
        make.right.offset(-12);
        make.top.equalTo(picView).offset(8);
    }];
    [goodsDescLabel mas_makeConstraints:^(MASConstraintMaker *make) {
        make.left.right.equalTo(goodsNameLabel);
        make.bottom.equalTo(picView).offset(-8);
    }];
    [callBtn mas_makeConstraints:^(MASConstraintMaker *make) {
        make.left.bottom.right.offset(0);
        make.height.offset(fk_adjustW(45));
    }];
    [separator mas_makeConstraints:^(MASConstraintMaker *make) {
        make.left.right.offset(0);
        make.height.offset(0.8);
        make.bottom.equalTo(callBtn.mas_top);
    }];
}

- (void)callBtnClicked
{
    if (self.data) {
        NSString *customer_phone = self.data[@"customer_phone"];
        if (customer_phone.length > 0) {
            fk_call(customer_phone);
        }
    }
}

- (void)setData:(id)data
{
    [super setData:data];
    
    if ([data isKindOfClass:[NSDictionary class]]) {
        [self.picView sd_setImageWithURL:[NSURL URLWithString:data[@"goods_image"]] placeholderImage:kPlaceholder_1x1];
        self.goodsNameLabel.text = data[@"goods_title"];
        self.goodsDescLabel.text = data[@"option_title"];
        
        NSMutableArray *values = [NSMutableArray array];
        [values addObject:data[@"refund_reason"]];
        [values addObject:fkFORMAT(@"￥%@", data[@"refund_price"])];
        [values addObject:data[@"goods_num"]];
        [values addObject:data[@"apply_datetime_string"]];
        [values addObject:data[@"refund_sn"]];
        for (UILabel *valueLabel in self.valueLabels) {
            NSInteger index = [self.valueLabels indexOfObject:valueLabel];
            valueLabel.text = values[index];
        }
    }
}

@end
